package com.kanshu.ksgb.fastread.doudou.common.util;

import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.view.ViewTreeObserver;
import android.widget.ScrollView;

import com.dl7.recycler.adapter.BaseQuickAdapter;
import com.lcodecore.tkrefreshlayout.IBottomView;
import com.lcodecore.tkrefreshlayout.RefreshListenerAdapter;
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout;
import com.lcodecore.tkrefreshlayout.header.progresslayout.ProgressLayout;

/**
 * 下拉刷新帮助类
 * Created by wcy on 2017/5/10.
 */
public class SwipeRefreshHelper {

    private SwipeRefreshHelper() {
        throw new AssertionError();
    }


    /**
     * 初始化
     * @param refreshLayout
     * @param listener
     */
    public static void init(SwipeRefreshLayout refreshLayout, SwipeRefreshLayout.OnRefreshListener listener) {
        refreshLayout.setColorSchemeResources(android.R.color.holo_blue_bright,
                android.R.color.holo_green_light,
                android.R.color.holo_orange_light,
                android.R.color.holo_red_light);
        refreshLayout.setOnRefreshListener(listener);
    }

    public static void init(TwinklingRefreshLayout refreshLayout, final SwipeRefreshLayout.OnRefreshListener listener) {
        refreshLayout.setFloatRefresh(true);
        refreshLayout.setEnableLoadmore(false);
        refreshLayout.setEnableOverScroll(true);
        ProgressLayout progressLayout = new ProgressLayout(refreshLayout.getContext());
        progressLayout.setColorSchemeResources(android.R.color.holo_blue_bright,
                android.R.color.holo_green_light,
                android.R.color.holo_orange_light,
                android.R.color.holo_red_light);
        refreshLayout.setHeaderView(progressLayout);
        refreshLayout.setOnRefreshListener(new RefreshListenerAdapter() {
            @Override
            public void onPullingDown(TwinklingRefreshLayout refreshLayout, float fraction) {
                super.onPullingDown(refreshLayout, fraction);
            }

            @Override
            public void onPullingUp(TwinklingRefreshLayout refreshLayout, float fraction) {
                super.onPullingUp(refreshLayout, fraction);
            }

            @Override
            public void onPullDownReleasing(TwinklingRefreshLayout refreshLayout, float fraction) {
                super.onPullDownReleasing(refreshLayout, fraction);
            }

            @Override
            public void onPullUpReleasing(TwinklingRefreshLayout refreshLayout, float fraction) {
                super.onPullUpReleasing(refreshLayout, fraction);
            }

            @Override
            public void onRefresh(TwinklingRefreshLayout refreshLayout) {
                super.onRefresh(refreshLayout);
                if (listener != null) {
                    listener.onRefresh();
                }
            }

            @Override
            public void onLoadMore(TwinklingRefreshLayout refreshLayout) {
                super.onLoadMore(refreshLayout);
            }

            @Override
            public void onFinishRefresh() {
                super.onFinishRefresh();
            }

            @Override
            public void onFinishLoadMore() {
                super.onFinishLoadMore();
            }

            @Override
            public void onRefreshCanceled() {
                super.onRefreshCanceled();
            }

            @Override
            public void onLoadmoreCanceled() {
                super.onLoadmoreCanceled();
            }
        });
    }

    public static void initTwinklingRefresh(TwinklingRefreshLayout refreshLayout, IBottomView footer, RefreshListenerAdapter listener) {
        refreshLayout.setFloatRefresh(true);
        ProgressLayout progressLayout = new ProgressLayout(refreshLayout.getContext());
        progressLayout.setColorSchemeResources(android.R.color.holo_blue_bright,
                android.R.color.holo_green_light,
                android.R.color.holo_orange_light,
                android.R.color.holo_red_light);
        refreshLayout.setHeaderView(progressLayout);
        refreshLayout.setBottomView(footer);
        refreshLayout.setOnRefreshListener(listener);
    }


    /**
     * 使能刷新
     * @param refreshLayout
     * @param isEnable
     */
    public static void enableRefresh(SwipeRefreshLayout refreshLayout, boolean isEnable) {
        if (refreshLayout != null) {
            refreshLayout.setEnabled(isEnable);
        }
    }

    /**
     * 控制刷新
     * @param refreshLayout
     * @param isRefresh
     */
    public static void controlRefresh(TwinklingRefreshLayout refreshLayout, boolean isRefresh) {
        if (refreshLayout != null) {
            refreshLayout.finishRefreshing();
        }
    }

    /**
     * 处理recyclerview与refreshLayout的冲突
     * @param refreshLayout
     * @param recyclerView
     */
    public static void handleSwipeConflict(final SwipeRefreshLayout refreshLayout, RecyclerView recyclerView) {
        recyclerView.setOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                int topRowVerticalPosition = (recyclerView == null || recyclerView.getChildCount() == 0) ? 0 : recyclerView.getChildAt(0).getTop();
                refreshLayout.setEnabled(topRowVerticalPosition >= 0);
            }
        });
    }

    public static int getRecyclerTopVertical(RecyclerView recyclerView) {
        return recyclerView.getChildCount() == 0 ? 0 : recyclerView.getChildAt(0).getTop();
    }

    /**
     * 处理scrollView与refreshLayout的冲突
     * @param refreshLayout
     * @param scrollView
     */
    public static void handleSwipeConflict(final SwipeRefreshLayout refreshLayout, final ScrollView scrollView) {
        scrollView.getViewTreeObserver().addOnScrollChangedListener(new ViewTreeObserver.OnScrollChangedListener() {
            @Override
            public void onScrollChanged() {
                if (refreshLayout != null) {
                    refreshLayout.setEnabled(scrollView.getScrollY() == 0);
                }
            }
        });
    }

    public static void swipeRefreshCompleted(SwipeRefreshLayout swipeRefreshLayout, RecyclerView.Adapter adapter) {
        if (swipeRefreshLayout != null) {
            swipeRefreshLayout.setRefreshing(false);
        }
        if (adapter != null && adapter instanceof BaseQuickAdapter) {
            ((BaseQuickAdapter) adapter).loadComplete();
        }
    }

    public static void swipeRefreshCompleted(TwinklingRefreshLayout swipeRefreshLayout, RecyclerView.Adapter adapter) {
        if (swipeRefreshLayout != null) {
            swipeRefreshLayout.finishRefreshing();
        }
        if (adapter != null && adapter instanceof BaseQuickAdapter) {
            ((BaseQuickAdapter) adapter).loadComplete();
        }
    }
}
